//
//  NYHomeHeaderView.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/3.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYHomeHeaderView.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"

#import "NYHomeFocusRequest.h"

#define HomeMenuButtonTag  7800
#define MENUWIDTH         [UIScreen mainScreen].bounds.size.width/4.f
#define HOMEFOCUS_HEIGHT  (IS_IPAD ? __WGHeight(286.f) : __WGHeight(143.f))

@interface NYHomeHeaderView () <SDCycleScrollViewDelegate>

@property (nonatomic, weak) id<NYHomeHeaderViewDelegate> delegate;

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UIView *menuView;

@property (nonatomic, strong) NSMutableArray *focusMutaArray;
//request
//@property (nonatomic, strong) NYHomeFocusRequest *focusRequest;

@end

@implementation NYHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame withDelegate:(id<NYHomeHeaderViewDelegate>)delegate {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = delegate;
        [self initUI];
    }
    return self;
}
#pragma mark - Intial Methods
- (void)initUI {
    
    self.backgroundColor = UIColorMakeWithHex(@"#F3F4F5");
    [self addSubview:self.cycleScrollView];
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(HOMEFOCUS_HEIGHT);
    }];
    
    [self addSubview:self.menuView];
    
    [self.menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self);
        make.top.equalTo(self.cycleScrollView.mas_bottom);
        make.bottom.equalTo(self);
    }];
    
    [self loadTheFocusMethod];
    
//    NSArray *arrayM = @[@{@"img":[UIImage xuan_ke_zhong_xin], @"title":@"选课中心"},
//  @{@"img":[UIImage yan_jiu_yuan_jie_shao], @"title":@"研究院介绍"}, @{@"img":[UIImage ge_ren_zhong_xin], @"title":@"个人中心"}, @{@"img":[UIImage hui_yuan_gou_mai], @"title":@"在线答疑"}];
    NSArray *arrayM = @[@{@"img":[UIImage xuan_ke_zhong_xin], @"title":@"选课中心"},
                        @{@"img":[UIImage yan_jiu_yuan_jie_shao], @"title":@"研究院介绍"}, @{@"img":[UIImage ge_ren_zhong_xin], @"title":@"个人中心"}, @{@"img":UIImageMake(@"shou_ye_da_yi"), @"title":@"在线答疑"}];

    @weakify(self);
    [arrayM enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * stop) {
        
        @strongify(self);
        [self allockButtonIndex:idx withDictionary:obj];
    }];
}
- (void)loadTheFocusMethod {
    
    @weakify(self);
    [[NYHomeFocusRequest requestWithParams:nil] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYHomeFocusModel *> * _Nonnull data) {
        
        @strongify(self);
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NYHomeFocusModel *focusModel in data) {
            [self.focusMutaArray addObject:focusModel];
            [arrayM addObject:focusModel.pic];
        }
        [self.cycleScrollView setImageURLStringsGroup:arrayM];
        NSLogInfo(@"%@",data);
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        
        NSLogError(@"%@",error);
    }];
}

#pragma mark - Target Methods
- (void)menuButtonSelectd:(QMUIButton *)btn {
    
    NSInteger index = btn.tag - HomeMenuButtonTag;
    
    if ([self.delegate respondsToSelector:@selector(clickOnTheButton:)]) {
        [self.delegate clickOnTheButton:index];
    }
}
#pragma mark - Public Methods

#pragma mark - Private Method

-(void)allockButtonIndex:(NSInteger)index withDictionary:(NSDictionary *)dictionary {
    
    QMUIButton *button = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [button setTag:(index + 1) + HomeMenuButtonTag];
    button.imagePosition = QMUIButtonImagePositionTop;
    [button setImage:dictionary[@"img"] forState:0];
    [button setTitle:dictionary[@"title"] forState:0];
    button.titleLabel.font = UIFontMake(13.f);
    [button setTitleColor:UIColorMakeWithHex(@"#333333") forState:0];
    button.imageEdgeInsets = UIEdgeInsetsMake(5, 0, 0, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(15, 0, 0, 0);
    [button setFrame:CGRectMake(index * MENUWIDTH, 0, MENUWIDTH, __WGHeight(86))];
    [button addTarget:self action:@selector(menuButtonSelectd:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuView addSubview:button];
}

#pragma mark - External Delegate

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
//    NYHomeFocusModel *model = self.focusMutaArray[index];
//    if ([self.delegate respondsToSelector:@selector(didSelectCycleScrollViewItem:)]) {
//        [self.delegate didSelectCycleScrollViewItem:model];
//    }
}
#pragma mark - Setter Getter Methods

- (SDCycleScrollView *)cycleScrollView {
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HOMEFOCUS_HEIGHT) delegate:self placeholderImage:[UIImage qmui_imageWithColor:UIColorMakeWithHex(@"#E5E5E5") size:CGSizeMake(SCREEN_WIDTH, HOMEFOCUS_HEIGHT) cornerRadius:0.f]];
        _cycleScrollView.autoScroll = YES;
        _cycleScrollView.titleLabelHeight = 25.f;
        _cycleScrollView.titleLabelTextFont = UIFontMake(14);
        _cycleScrollView.autoScrollTimeInterval = 3.f;
        _cycleScrollView.pageDotColor = [UIColor whiteColor];
        _cycleScrollView.currentPageDotColor = UIColorMakeWithHex(@"#5396FF");
        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _cycleScrollView.autoresizingMask = YES;
    }
    return _cycleScrollView;
}
- (UIView *)menuView {
    if (!_menuView) {
        _menuView = [[UIView alloc] initWithFrame:CGRectMake(0, HOMEFOCUS_HEIGHT, SCREEN_WIDTH, CGRectGetHeight(self.frame) - HOMEFOCUS_HEIGHT - 10.f)];
        _menuView.backgroundColor = UIColorMakeWithHex(@"#FFFFFF");
    }
    return _menuView;
}
- (NSMutableArray *)focusMutaArray {
    if (!_focusMutaArray) {
        _focusMutaArray = [NSMutableArray array];
    }
    return _focusMutaArray;
}

@end
